package com.sinsz.c.dubbo;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.sinsz.c.properties.DubboProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.StringUtils;

/**
 * 阿里巴巴分布式治理框架
 * <p>
 *     dubbo.xml为默认配置暴露服务和获取服务的配置文件。
 * </p>
 * @author chenjianbo
 * @date 2017/11/23
 */
@Configuration
@Lazy(false)
@EnableConfigurationProperties(value = {DubboProperties.class})
public class DubboConfiguration {

    private final DubboProperties dubboProperties;

    private static final Logger logger = LoggerFactory.getLogger(DubboConfiguration.class);

    public DubboConfiguration(DubboProperties dubboProperties) {
        this.dubboProperties = dubboProperties;
        logger.info("dubbo配置：{}", this.dubboProperties);
    }

    /**
     * 服务提供方应用名
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig application = new ApplicationConfig();
        application.setName(dubboProperties.getApplication().getName());
        application.setRegistry(registryConfig());
        return application;
    }

    /**
     * 注册中心地址及协议
     * @return
     */
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress(
                dubboProperties.getRegistry().getAddress()
        );
        registry.setCheck(
                dubboProperties.isCheck()
        );
        String user = dubboProperties.getRegistry().getUsername();
        String pwd = dubboProperties.getRegistry().getPassword();
        if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(pwd)) {
            registry.setUsername(user);
            registry.setPassword(pwd);
        }
        return registry;
    }

    /**
     * 暴露服务方式与端口
     * @return
     */
    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName(
                dubboProperties.getProtocol().getName()
        );
        if (!StringUtils.isEmpty(dubboProperties.getProtocol().getHost())) {
            protocol.setHost(
                    dubboProperties.getProtocol().getHost()
            );
        }
        protocol.setPort(
                dubboProperties.getProtocol().getPort()
        );
        return protocol;
    }

    /**
     * 消费者启动禁用检查
     * @return
     */
    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumer = new ConsumerConfig();
        consumer.setCheck(
                dubboProperties.isCheck()
        );
        return consumer;
    }

}